package com.flashflexpro.flexdirector.adapter.codec {
import com.flashflexpro.flexdirector.adapter.descriptor.DescPropertyAuto;

import com.flashflexpro.flexdirector.model.CodeStringValue;

import mx.automation.IAutomationObject;

public class CodecKeyModifier extends CodecDefault{

    override public function encode(obj:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject ):CodeStringValue{
        var str:String = "";
        if ("ctrlKey" in obj && Boolean(obj["ctrlKey"])) {
            str += "ctrl";
        }

        if ("shiftKey" in obj && Boolean(obj["shiftKey"])) {
            if( str.length > 0 ){
                str += ";";
            }
            str += "shift";
        }

        if ("altKey" in obj && Boolean(obj["altKey"])) {
            if( str.length > 0 ){
                str += ";";
            }
            str += "alt";
        }

        var rt:CodeStringValue = new CodeStringValue();
        rt.desc = descProperty.getDto();
        rt.string = str;
        rt.value = str;
        rt.code = "\"" + str + "\"";
        return rt;
    }

    override public function decode(obj:Object, value:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject, inString:Boolean = false):void {
        var str:String = value as String;
        if ( str.indexOf( "ctrl" ) >= 0 ) {
            obj["ctrlKey"] = true;
        }
        if ( str.indexOf( "shift" ) >= 0 ) {
            obj["shiftKey"] = true;
        }
        if ( str.indexOf( "alt" ) >= 0 ) {
            obj["altKey"] = true;
        }
    }
}
}